package org.corey.wyp.business;

import java.util.ArrayList;
import java.util.List;

import org.corey.wyp.vo.AccountItem;
import org.corey.wyp.vo.Calendar;
import org.corey.wyp.vo.DaySetting;
import org.corey.wyp.vo.DaySettingTemplate;
import org.corey.wyp.vo.Month;
import org.corey.wyp.vo.MonthSetting;
import org.corey.wyp.vo.MonthSettingTemplate;
import org.corey.wyp.vo.SettingTemplate;
import org.corey.wyp.vo.SettingTemplateType;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class ProperityManager {
	private HibernateTemplate template;

	public void setSessionFactory(SessionFactory sf) {
		template = new HibernateTemplate(sf);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<Calendar> getCalendars() {
		List<Calendar> list = template.find("from Calendar");
		return list;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public Calendar getCalendarAccount(final int year, final int month,
			final int date) {
		List<Calendar> list=(List<Calendar>) template.find("from Calendar c where c.year="+year+" and c.month="+month+" and c.date="+date);
		Calendar c;
		if(list==null || list.size()==0){
			c=new Calendar();
			c.setYear(year);
			c.setMonth(month);
			c.setDate(date);
			this.saveCalendar(c);
			return c;
		}else{
			c=list.get(0);
		}
		return c;
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public boolean saveCalendar(Calendar calendar) {
			template.save(calendar);
			return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean removeCalendar(Integer id) {
		Calendar c = new Calendar();
		c.setId(id);
		template.delete(c);
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Integer saveAccountItem(AccountItem accountItem) {
		System.out.println("save Account Item");
		template.saveOrUpdate(accountItem);
		return accountItem.getId();
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean removeAccountItem(List<Integer> ids) {
		for (Integer i = 0; i < ids.size(); i++) {
			AccountItem item = (AccountItem) template.get(AccountItem.class,
					ids.get(i));
			template.delete(item);
		}
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public MonthSetting saveMonthSetting(MonthSetting monthSetting) {
		template.saveOrUpdate(monthSetting);
		return monthSetting;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public MonthSetting getMonthSetting(int year, int month) {
		List<MonthSetting> list = template
				.find("from MonthSetting m where m.year=" + year
						+ " and m.month=" + month);
		if (list == null || list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/******************************** SettingTemplate operation *********************/
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean removeDaySettingTemplate(Integer settingId) {
		DaySettingTemplate setting = new DaySettingTemplate();
		setting.setId(settingId);
		template.delete(setting);
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean removeMonthSettingTemplate(Integer settingId) {
		MonthSettingTemplate setting = new MonthSettingTemplate();
		setting.setId(settingId);
		template.delete(setting);
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean saveDaySettingTemplate(DaySettingTemplate daySettingTemplate) {
		template.save(daySettingTemplate);
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean saveMonthSettingTemplate(
			MonthSettingTemplate monthSettingTemplate) {
		template.save(monthSettingTemplate);
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateDaySettingTemplate(
			DaySettingTemplate daySettingTemplate) {
		template.update(daySettingTemplate);
		return true;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateMonthSettingTemplate(
			MonthSettingTemplate monthSettingTemplate) {
		template.update(monthSettingTemplate);
		return true;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public SettingTemplate saveSettingTemplate(String settingType,SettingTemplate settingTemplate){
		SettingTemplate st=null;
		try {
			if(SettingTemplateType.MONTH.equals(settingType)){
				MonthSettingTemplate mst=new MonthSettingTemplate();
				mst.setSettingName(settingTemplate.getSettingName());
				MonthSetting ms=(MonthSetting)(((MonthSettingTemplate)settingTemplate).getMonthSetting().clone());
				mst.setMonthSetting(ms);
				template.save(mst);
				st=mst;
			}else if(SettingTemplateType.DAY.equals(settingType)){
				DaySettingTemplate dst=new DaySettingTemplate();
				dst.setSettingName(settingTemplate.getSettingName());
				DaySetting ds=(DaySetting)(((DaySettingTemplate)settingTemplate).getDaySetting().clone());
				dst.setDaySetting(ds);
				template.save(dst);
				st=dst;
			}
		} catch (CloneNotSupportedException e) {
			
		}
		return st;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED,readOnly=true)
	public List<SettingTemplate> getSettingTemplate(String settingType){
		List<SettingTemplate> templates=new ArrayList<SettingTemplate>();
		if(SettingTemplateType.MONTH.equals(settingType)){
			templates=template.find("from "+MonthSettingTemplate.class.getSimpleName()+"");
		}else if(SettingTemplateType.DAY.equals(settingType)){
			templates=template.find("from "+DaySettingTemplate.class.getSimpleName()+"");
		}
		return templates;
	}

	/******************************** SettingTemplate operation *********************/
	public String testConnection(String seedString) {
		return "server response:" + seedString;
	}

	public Calendar getServerTime() {
		System.out.println("server:getServerTime");
		Calendar today = new Calendar();
		java.util.Calendar c = java.util.Calendar.getInstance();
		int year = c.get(java.util.Calendar.YEAR);
		int month = c.get(java.util.Calendar.MONTH)+1;
		int date = c.get(java.util.Calendar.DATE);
		if ((today = this.getCalendarAccount(year, month, date)) == null) {
			today = new Calendar();
			today.setYear(year);
			today.setMonth(month);
			today.setDate(date);
			this.saveCalendar(today);
		}
		return today;
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED,readOnly=true)
	public Month getMonth(final int year, final int month) {
		List<Calendar> calendars = (List<Calendar>) template.find("from Calendar c WHERE c.year="+year+" AND c.month="+month);			
		Month m = new Month();
		m.setYear(year);
		m.setMonth(month);
		m.setDays(calendars);
		return m;
	}
	
	public static void main(String[] args) {
	}
}
